package com.situ.mall.collection;

import org.junit.jupiter.api.Test;

import java.util.*;

public class Demo {

    @Test
    public void test1() {
        Collection<String> collection = new ArrayList<>();
        collection.add("张三");
        collection.add("李四");
        collection.add("王五");
        collection.add("张三");
        Object[] array = collection.toArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("--------------------");
        for (String str : collection) {
            System.out.println(str);
        }
        System.out.println("--------------------");
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            String data = iterator.next();
            System.out.println(data);
        }
    }

    @Test
    public void test2() {
        List<String> list = new ArrayList<String>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        list.add(2, "Java");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            //java.util.ConcurrentModificationException
            if (str.equals("abc3")) {
                list.add("ABC3");
            }
            System.out.println(str);
        }
    }

    @Test
    public void test4() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String data = iterator.next();
            if (data.equals("3")) {
                //list.remove("3");
                iterator.remove();
            }
        }
        System.out.println(list.size());

    }

    @Test
    public void test551() {
        //hashCode 没有冲突，直接放进去，有冲突，计算equals
        // 创建HashSet对象
        Set<String> set = new HashSet<String>();
        // 给集合中添加自定义对象
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhangsan");
        set.add(new String("lisi"));
        for (String string : set) {
            System.out.println(string);
        }
    }

    @Test
    public void test12() {
        Set<Student> set = new HashSet<>();
        Student stu1 = new Student(1, "张三1", 20);
        Student stu2 = new Student(1, "张三2", 20);
        set.add(stu1);
        set.add(stu2);
        for (Student student : set) {
            System.out.println(student);
        }
    }

    @Test
    public void test423() {
        Set<String> set = new LinkedHashSet<String>();
        set.add("bbb");
        set.add("aaa");
        set.add("abc");
        set.add("bbc");
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    @Test
    public void test5() {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student(1, "张三", 23));
        treeSet.add(new Student(2, "李四", 13));
        treeSet.add(new Student(3, "周七", 13));
        treeSet.add(new Student(4, "王五", 43));
        treeSet.add(new Student(5, "赵六", 33));

        System.out.println(treeSet);
    }

    @Test
    public void test444() {
        System.out.println("Aa".hashCode());//2112
        System.out.println("BB".hashCode());//2112
    }

    @Test
    public void test55() {
        Student s1 = new Student();
        int h1 = s1.hashCode();
        System.out.println(h1);//1983025922
        System.out.println(s1);//com.situ.mall.collection.Student@76329302
        System.out.println(Integer.toHexString(h1));//76329302

        Student s2 = new Student();
        int h2 = s2.hashCode();
        System.out.println(h2);

        System.out.println(s1 == s2);//false

    }

    @Test
    public void test4244() {
        List<String> list = new ArrayList<>();
        list.add("a");
    }
}
